package com.bindapp.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;

import com.bindapp.R;
import com.bindapp.adapter.ViewPagerAdapter;
import com.bindapp.databinding.ActivityTabBinding;
import com.bindapp.fragment.MainTab1Fragment;
import com.bindapp.fragment.MainTab2Fragment;
import com.bindapp.fragment.MainTab3Fragment;
import com.bindapp.fragment.MainTab4Fragment;
import com.bindapp.util.AppManager;
import com.bindapp.util.BottomNavigationViewHelper;
import com.bindapp.util.Static;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

public class TabActivity extends AppCompatActivity {
    private ViewPagerAdapter adapter;
    private List<Fragment> mlist;
    private ActivityTabBinding binding;

    protected ImmersionBar mImmersionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_tab);

        initImmersionBar();
        initView();
        AppManager.addActivity(this);

    }

    private void initView(){
        mlist = new ArrayList<Fragment>();
        mlist.add(new MainTab1Fragment());
        mlist.add(new MainTab2Fragment());
        mlist.add(new MainTab3Fragment());
        mlist.add(new MainTab4Fragment());
        adapter = new ViewPagerAdapter(getSupportFragmentManager(),mlist);
        binding.viewpager.setAdapter(adapter);
        binding.viewpager.setOffscreenPageLimit(4);

        BottomNavigationViewHelper.disableShiftMode(binding.navigationbar);

        binding.navigationbar.setOnNavigationItemSelectedListener(new BottomTabClick());

    }

    class BottomTabClick implements BottomNavigationView.OnNavigationItemSelectedListener {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_tab1:
                    invalidateOptionsMenu();
                    binding.viewpager.setCurrentItem(0);

                    //StatusBarCompat.setStatusBarColor(TabActivity.this, ContextCompat.getColor(TabActivity.this,R.color.tab2bg),true);
                    break;
                case R.id.action_tab2:
                    invalidateOptionsMenu();
                    binding.viewpager.setCurrentItem(1);
                    //StatusBarCompat.setStatusBarColor(TabActivity.this, ContextCompat.getColor(TabActivity.this,R.color.tab2bg),true);
                    break;
                case R.id.action_tab3:
                    invalidateOptionsMenu();
                    binding.viewpager.setCurrentItem(2);
                    //StatusBarCompat.setStatusBarColor(TabActivity.this, ContextCompat.getColor(TabActivity.this,R.color.tab2bg),true);
                    break;
                case R.id.action_tab4:
                    invalidateOptionsMenu();
                    binding.viewpager.setCurrentItem(3);
                    //StatusBarCompat.setStatusBarColor(TabActivity.this, ContextCompat.getColor(TabActivity.this,R.color.tab4bar),false);
                    break;
            }
            return true;
        }
    }

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.fitsSystemWindows(true).keyboardEnable(true).statusBarDarkFont(true).init();
        //mImmersionBar.keyboardEnable(true).keyboardEnable(true).statusBarDarkFont(true).init();
        //mImmersionBar.fitsSystemWindows(true).keyboardEnable(true).statusBarDarkFont(true).init();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            Static.myLog("按下了back键   onKeyDown()");
            finish();
            return false;
        }else {
            return super.onKeyDown(keyCode, event);
        }

    }
}
